﻿using Inovout.Cloud.Applications.Security.Models;
using Inovout.Cloud.Applications.Security.Services;
using Inovout.Cloud.Console.Web.Models;
using Inovout.Cloud.Web.Mvc;
using System.Linq;
using System.Web.Mvc;

namespace Inovout.Cloud.Console.Web.Controllers
{
    public class CredentialController<TCredential> : CloudController<TCredential> where TCredential : class,IApplicationCredential
    {
        protected ApplicationUser ApplicationUser { get; private set; }
        protected override void Initialize()
        {
            base.Initialize();

            if (Project!=null)
            {
                ApplicationUser = ComponentRegistry.Resolve<IApplicationUserService>().FindByApplication((Inovout.Cloud.Applications.Models.Application)base.Project.Applications.ToArray()[0]);
                ViewBag.ApplicationUser = ApplicationUser;
            }
        }

        protected override object WrapJsonObect(TCredential credential)
        {
            credential.ApplicationUser = null;
            return new ApplicationCredentialInfo<TCredential>(credential);
        }
    }
}